package com.design.working.controller;

import com.design.working.config.BaseServlet;
import com.design.working.dto.LoginDTO;
import com.design.working.entity.Admin;
import com.design.working.entity.Counselor;
import com.design.working.entity.DeptEmployee;
import com.design.working.entity.Student;
import com.design.working.service.DeptService;
import com.design.working.service.LoginService;
import com.design.working.service.UserService;
import com.design.working.service.impl.DeptServiceImpl;
import com.design.working.service.impl.LoginServiceImpl;
import com.design.working.service.impl.UserServiceImpl;
import com.design.working.vo.ResponseVO;
import com.google.gson.Gson;
import org.apache.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * @author gavin
 * @date 2022/5/7 18:59
 */
@WebServlet(name = "UserController",
            urlPatterns = {"/user/*"})
public class UserController extends BaseServlet {

    private final LoginService loginService = new LoginServiceImpl();

    private final UserService userService = new UserServiceImpl();

    Logger log = Logger.getLogger(UserController.class);

    /**
     * 登录
     */
    public void login(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        response.setContentType("text/html; charset=UTF-8");
        request.setCharacterEncoding("UTF-8");

        Gson gson = new Gson();
        HttpSession session = request.getSession();

        LoginDTO loginDTO = new LoginDTO();
        loginDTO.setUid(Integer.valueOf(request.getParameter("uid")));
        loginDTO.setPassword(request.getParameter("password"));
        loginDTO.setIdentity(request.getParameter("identity"));


        switch (loginDTO.getIdentity()) {
            case "student":
                ResponseVO<Student> studentResponseVO = loginService.studentLogin(loginDTO);
                if(studentResponseVO.getStatus() == 0) {
                    session.setAttribute("uid",studentResponseVO.getData().getUid());
                    session.setAttribute("username",studentResponseVO.getData().getUsername());
                    session.setAttribute("userType", "student");
                    response.getWriter().print(gson.toJson("jsp/student/student_main.jsp"));
                    log.info("学生：" + loginDTO.getUid() + " 登录");
                }
                else {
                    response.getWriter().print(studentResponseVO.getStatus());
                    log.info("学号：" + loginDTO.getUid() + " 尝试登录，失败");
                }
                break;
            case "counselor":
                ResponseVO<Counselor> counselorResponseVO = loginService.counselorLogin(loginDTO);
                if(counselorResponseVO.getStatus() == 0) {
                    session.setAttribute("uid",counselorResponseVO.getData().getUid());
                    session.setAttribute("username",counselorResponseVO.getData().getUsername());
                    session.setAttribute("userType", "counselor");
                    response.getWriter().print(gson.toJson("jsp/counselor/counselor_main.jsp"));
                    log.info("辅导员：" + loginDTO.getUid() + " 登录");
                }
                else {
                    response.getWriter().print(counselorResponseVO.getStatus());
                    log.info("辅导员：" + loginDTO.getUid() + " 尝试登录，失败");
                }
                break;
            case "dept":
                ResponseVO<DeptEmployee> deptEmployeeResponseVO = loginService.deptEmployeeLogin(loginDTO);
                if(deptEmployeeResponseVO.getStatus() == 0) {
                    session.setAttribute("uid",deptEmployeeResponseVO.getData().getUid());
                    session.setAttribute("username",deptEmployeeResponseVO.getData().getUsername());
                    session.setAttribute("userType", "dept");

                    //先判断该部门员工是否是部门领导
                    DeptService deptService = new DeptServiceImpl();
                    if (!deptService.isLeaderToDept(deptEmployeeResponseVO.getData().getUid())){
                        response.getWriter().print(gson.toJson("jsp/dept/dept_employee_main.jsp"));
                        log.info("用工部门员工：" + loginDTO.getUid() + " 登录");
                        session.setAttribute("deptAdminJspType", "dept_employee_main.jsp");
                    } else {
                        response.getWriter().print(gson.toJson("jsp/dept/dept_leader_main.jsp"));
                        log.info("用工部门领导：" + loginDTO.getUid() + " 登录");
                        session.setAttribute("deptAdminJspType", "dept_leader_main.jsp");
                    }
                }
                else {
                    response.getWriter().print(deptEmployeeResponseVO.getStatus());
                    log.info("用工部门员工：" + loginDTO.getUid() + " 尝试登录，失败");
                }
                break;
            case "admin":
                ResponseVO<Admin> adminResponseVO = loginService.adminLogin(loginDTO);
                if(adminResponseVO.getStatus() == 0) {
                    session.setAttribute("uid",adminResponseVO.getData().getUid());
                    session.setAttribute("username",adminResponseVO.getData().getUsername());
                    session.setAttribute("userType", "admin");
                    response.getWriter().print(gson.toJson("jsp/admin/admin_main.jsp"));
                    log.info("管理员：" + loginDTO.getUid() + " 登录");
                }
                else {
                    response.getWriter().print(adminResponseVO.getStatus());
                    log.info("管理员：" + loginDTO.getUid() + " 尝试登录，失败");
                }
                break;
            default:
        }
    }

    /**
     * 退出登录
     */
    public void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html");
        HttpSession session = request.getSession();
        session.invalidate();
        response.sendRedirect("/index.jsp");
    }

    /**
     * 修改密码
     */
    public void updatePassword(HttpServletRequest request, HttpServletResponse response) throws IOException {

        response.setContentType("text/html; charset=UTF-8");
        request.setCharacterEncoding("UTF-8");

        HttpSession session = request.getSession();
        String password = request.getParameter("secondPassword");
        Integer id = (Integer) session.getAttribute("uid");
        String type = (String) session.getAttribute("userType");

        switch (type) {
            case "student":
                response.getWriter().print(userService.updateStudentPassword(id, password));
                break;
            case "counselor":
                response.getWriter().print(userService.updateCounselorPassword(id, password));
                break;
            case "dept":
                response.getWriter().print(userService.updateDeptEmployeePassword(id, password));
                break;
            case "admin":
                response.getWriter().print(userService.updateAdminPassword(id, password));
                break;
            default:
        }
    }

    /**
     * 查看个人信息
     */
    public void selectInfo(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {

    }
}
